import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';
import 'package:x3_business_core/common_models/business_attribute_model.dart';

import 'business_attribute_item.dart';


class BusinessObjectAttribute extends StatelessWidget {
  final List<BusinessAttributeModel> attributeList;

  const BusinessObjectAttribute({Key key, @required this.attributeList}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BaseCardComponent(
      margin: EdgeInsets.fromLTRB(16.0, 12.0, 16.0, 12.0),
      child: ListView.separated(
        shrinkWrap: true,
        itemCount: attributeList.length,
        itemBuilder: (BuildContext context, int index) => BusinessAttributeItem(attributeModel: attributeList[index], fixedWidth: getMaxSize(context)),
        separatorBuilder: (BuildContext context, int index) => SizedBox(height: 12),
      ),
    );
  }

  double getMaxSize(BuildContext context) {
    String maxLabel = '';
    this.attributeList.forEach((element) {
      String maxElement = '';
      element.itemList.forEach((element) {
        String desc = element.attribute.key;
        if (desc.length > maxElement.length) maxElement = desc;
      });
      if (maxElement.length > maxLabel.length) maxLabel = maxElement;
    });
    Size size = BaseUtils.boundingTextSize(context, maxLabel, TextStyle(fontSize: 13, color: Color(0xFF868686)));
    return size.width;
  }
}
